package test.base.kv;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import hyl.base.kv.KVConfigs;
import hyl.base.kv.KVDb;
import hyl.core.MyFun;
import hyl.core.info.MyKV;
import hyl.core.run.IDoEachT;


public class testkv {
	void print(Object obje) {
		System.out.println(obje);
	}

	class User implements Serializable {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		String name;
		int age;

		public User(String name_, int age_) {
			name = name_;
			age = age_;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public int getAge() {
			return age;
		}

		public void setAge(int age) {
			this.age = age;
		}

		public String toString() {
			return MyFun.join("姓名:", name, ";年龄=", age);
		}

	}	

	@Test
	public void test2() {
		KVDb _db = KVConfigs.getKV("网络库");
		long n = MyFun.getMs();
		byte[] bb1="批量v1".getBytes();
		byte[] bb2="批量v2".getBytes();
		System.out.println("==1 写入===");
		_db.putB(bb1,  "批量v1111".getBytes());
		_db.putB(bb2,  "批量v1112".getBytes());
		System.out.println("==1 读取==="+ new String(_db.getB(bb1)));
		System.out.println("==1 读取==="+ new String(_db.getB(bb2)));
	}

	// 测试kv 的 配置和函数
	//@Test
	public void test1() {
		List<byte[]> list;
		Map<byte[], byte[]> maps;
		Map<byte[], byte[]> map;
		HashMap<String, byte[]> map1 = new HashMap<>();
		map1.put("批量a1", "批量v1".getBytes());
		map1.put("批量b2", "批量v2".getBytes());
		map1.put("批量c3", "批量v3".getBytes());
		map1.put("批量d4", "批量v4".getBytes());
		HashMap<byte[], byte[]> map2 = new HashMap<>();
		map2.put("批量1".getBytes(), "批量v1".getBytes());
		map2.put("批量2".getBytes(), "批量v2".getBytes());
		map2.put("批量3".getBytes(), "批量v3".getBytes());
		map2.put("批量4".getBytes(), "批量v4".getBytes());

		Map<String, byte[]> map3 = new HashMap<>();
		map3.put("批量a1", "批量v1".getBytes());
		map3.put("批量b2", "批量v2".getBytes());
		map3.put("批量b3", "批量v3".getBytes());
		map3.put("批量d4", "批量v4".getBytes());

		Map<Integer, byte[]> map4 = new HashMap<>();
		map4.put(1, "批量v1".getBytes());
		map4.put(2, "批量v2".getBytes());
		map4.put(3, "批量v3".getBytes());
		map4.put(4, "批量v4".getBytes());

		List<byte[]> list1 = new ArrayList<>();
		list1.add("批量a1".getBytes());
		list1.add("批量b2".getBytes());
		list1.add("批量c3".getBytes());

		List<String> list2 = new ArrayList<>();
		list2.add("批量a1");
		list2.add("批量b2");
		list2.add("批量c3");

		List<User> list3 = new ArrayList<>();
		list3.add(new User("你", 8));
		list3.add(new User("好", 18));
		list3.add(new User("吗", 28));

		IDoEachT<byte[], byte[]> call1 = new IDoEachT<byte[], byte[]>() {
			@Override
			public boolean isOver(byte[] key, byte[] val) throws Exception {
				System.out.println(MyFun.join("-1-", MyFun.bytes2U8str(key), ",", MyFun.bytes2U8str(val)));
				return false;
			}
		};
		IDoEachT<String, byte[]> call2 = new IDoEachT<String, byte[]>() {
			@Override
			public boolean isOver(String key, byte[] val) throws Exception {
				System.out.println(MyFun.join("-2-", key, ",", MyFun.bytes2U8str(val)));
				return false;
			}
		};
		IDoEachT<Integer, byte[]> call3 = new IDoEachT<Integer, byte[]>() {
			@Override
			public boolean isOver(Integer key, byte[] val) throws Exception {
				System.out.println(MyFun.join("-3-", key, ",", MyFun.bytes2U8str(val)));
				return false;
			}
		};
		IDoEachT<Long, byte[]> call4 = new IDoEachT<Long, byte[]>() {
			@Override
			public boolean isOver(Long key, byte[] val) throws Exception {
				System.out.println(MyFun.join("-3-", key, ",", MyFun.bytes2U8str(val)));
				return false;
			}
		};

//////////////////////////////////////////////////////////////////////////////

		KVDb _db = KVConfigs.getKV("网络库");
		long n = MyFun.getMs();
		System.out.println("==1 写入===");
		_db.putS("kk", MyFun.u8str2Bytes("vv"));
		_db.putS("键1", MyFun.u8str2Bytes("值1"));
		print(MyFun.bytes2U8str(_db.getS("kk")));
		print(MyFun.bytes2U8str(_db.getS("键1")));
		System.out.println("==1.1===");
		_db.putB("kk".getBytes(), "vv2".getBytes());
		_db.putB("键1".getBytes(), "值2".getBytes());
		print(MyFun.bytes2U8str(_db.getB("kk".getBytes())));
		print(MyFun.bytes2U8str(_db.getB("键1".getBytes())));

		System.out.println("==1.2===");

		_db.clear("p/user/".getBytes());
		_db.values("p/user/".getBytes()).forEach((v) -> System.out.println(MyFun.bytes2U8str(v)));
		System.out.println("==1.3===");

		_db.putBb("p/user/".getBytes(), "键bb1".getBytes(), "值bb1".getBytes());
		_db.putBb("p/user/".getBytes(), "键bb2".getBytes(), "值bb2".getBytes());
		// kv.getLike("p/user/".getBytes()).forEach(System.out::println);
		_db.values("p/user/".getBytes()).forEach((v) -> System.out.println(MyFun.bytes2U8str(v)));
		System.out.println("==1.4 写入===");
		_db.clear();
		_db.putSt("戳", "值1".getBytes());
		_db.putBt("戳".getBytes(), "值2".getBytes());
		maps = _db.getAll();
		maps.forEach((k, v) -> System.out.println(MyFun.join(MyFun.bytes2U8str(k), ",", MyFun.bytes2U8str(v))));

		System.out.println("==1.5 写入===");
		_db.clear();
		_db.putL(23, "值1".getBytes());
		_db.putSt("可看", "看到".getBytes());
		_db.putSb("可看", "看到".getBytes(), "老虎".getBytes());

		System.out.println(MyFun.bytes2U8str(_db.getSs("可看", "看到")));
		System.out.println(MyFun.bytes2U8str(_db.getSb("可看", "看到".getBytes())));
		System.out.println(MyFun.bytes2U8str(_db.getBb("可看".getBytes(), "看到".getBytes())));

		System.out.println(MyFun.bytes2U8str(_db.getL(23)));
		maps = _db.getAll();
		maps.forEach((k, v) -> System.out.println(MyFun.join(MyFun.bytes2U8str(k), ",", MyFun.bytes2U8str(v))));

		System.out.println("==2 删除===");
		_db.clear();

		_db.putManyS(map1);
		_db.delB("批量a1".getBytes());
		list = _db.values();
		list.forEach((v) -> System.out.println(MyFun.bytes2U8str(v)));
		System.out.println("==2.1 删除===");

		_db.delS("批量b2");
		list = _db.values();
		list.forEach((v) -> System.out.println(MyFun.bytes2U8str(v)));
		System.out.println("==2.2 删除===");
		_db.clear();
		_db.putManyS(map1);
		_db.delSs("批量", "a1");
		_db.delSb("批量", "b2".getBytes());
		_db.delBb("批量".getBytes(), "d4".getBytes());
		map = _db.getAll();
		map.forEach((k, v) -> System.out.println(MyFun.join(MyFun.bytes2U8str(k), ",", MyFun.bytes2U8str(v))));
		System.out.println("==2.3 删除===");
		_db.clear();

		_db.putManyS(map1);
		_db.clear("批量b".getBytes());
		map = _db.getAll();
		map.forEach((k, v) -> System.out.println(MyFun.join(MyFun.bytes2U8str(k), ",", MyFun.bytes2U8str(v))));

		System.out.println("==2.4===");

		_db.getDelS("p/user/键bb1");
		_db.putBb("p/user/".getBytes(), "键bb3".getBytes(), "值bb3".getBytes());
		// kv.getLike("p/user/".getBytes()).forEach(System.out::println);
		_db.values("p/user/".getBytes()).forEach((v) -> System.out.println(MyFun.bytes2U8str(v)));
		System.out.println("==2.4.1===");

		_db.getDelB("p/user/键bb2".getBytes());
		_db.putBb("p/user/".getBytes(), "键bb4".getBytes(), "值bb4".getBytes());
		// kv.getLike("p/user/".getBytes()).forEach(System.out::println);
		_db.values("p/user/".getBytes()).forEach((v) -> System.out.println(MyFun.bytes2U8str(v)));
		System.out.println("==2.5===");
		MyKV<byte[], byte[]> kv = _db.takeSb("p/user/");
		System.out.println(MyFun.join("取出", MyFun.bytes2U8str(kv.getKey()), ",", MyFun.bytes2U8str(kv.getValue())));

		_db.putBb("p/user/".getBytes(), "键bb5".getBytes(), "值bb5".getBytes());
		// kv.getLike("p/user/".getBytes()).forEach(System.out::println);
		_db.values("p/user/".getBytes()).forEach((v) -> System.out.println(MyFun.bytes2U8str(v)));
		System.out.println("==2.5.1===");
		_db.putSs("p/user/", "1", "1".getBytes());
		_db.putSs("p/user/", "2", "2".getBytes());
		_db.putSs("p/user/", "3", "3".getBytes());
		MyKV<String, byte[]> kv2 = _db.takeSs("p/user/");
		System.out.println(MyFun.join("取出", kv2.getKey(), ",", MyFun.bytes2U8str(kv2.getValue())));
		kv2 = _db.takeSs("p/user/");
		System.out.println(MyFun.join("取出", kv2.getKey(), ",", MyFun.bytes2U8str(kv2.getValue())));
		kv2 = _db.takeSs("p/user/");
		System.out.println(MyFun.join("取出", kv2.getKey(), ",", MyFun.bytes2U8str(kv2.getValue())));
		kv2 = _db.takeSs("p/user/");
		System.out.println(MyFun.join("取出", kv2.getKey(), ",", MyFun.bytes2U8str(kv2.getValue())));

		_db.values("p/user/".getBytes()).forEach((v) -> System.out.println(MyFun.bytes2U8str(v)));

		System.out.println("==2.5.2===");
		_db.clear();
		_db.putBt("p/user/".getBytes(), "1".getBytes());
		_db.putBt("p/user/".getBytes(), "2".getBytes());
		_db.putT("3".getBytes());
		MyKV<Long, byte[]> kv1 = _db.takeSt("p/user/");
		System.out.println(MyFun.join("取出", kv1.getKey(), ",", MyFun.bytes2U8str(kv1.getValue())));
		kv1 = _db.takeSt("p/user/");
		System.out.println(MyFun.join("取出", kv1.getKey(), ",", MyFun.bytes2U8str(kv1.getValue())));
		maps = _db.getAll();
		maps.forEach((k, v) -> System.out.println(MyFun.join(MyFun.bytes2U8str(k), ",", MyFun.bytes2U8str(v))));
		System.out.println("==2.5.3===");
		_db.clear();
		_db.putBs("p/user/".getBytes(), "a", "aaa".getBytes());
		_db.putBs("p/user/".getBytes(), "b", "bbb".getBytes());
		_db.putBs("p/user/".getBytes(), "c", "ccc".getBytes());
		byte[][] bb = new byte[][] { "p/user/a".getBytes(), "p/user/b".getBytes() };

		_db.getManyB(Arrays.asList(bb))
				.forEach((k, v) -> System.out.println(MyFun.join(MyFun.bytes2U8str(k), ",", MyFun.bytes2U8str(v))));

		System.out.println("==2.5.4===");
		_db.clear();
		_db.putBs("p/user/".getBytes(), "a", "aaa".getBytes());
		_db.putBs("p/user/".getBytes(), "b", "bbb".getBytes());
		_db.putBs("p/user/".getBytes(), "c", "ccc".getBytes());
		String[] bc = new String[] { "p/user/a", "p/user/b" };

		_db.getManyS(Arrays.asList(bc)).forEach((k, v) -> System.out.println(MyFun.join(k, ",", MyFun.bytes2U8str(v))));
		System.out.println("==2.5.5===");
		byte[][] bd = new byte[][] { "a".getBytes(), "b".getBytes() };
		_db.getManyBb("p/user/".getBytes(), Arrays.asList(bd))
				.forEach((k, v) -> System.out.println(MyFun.join(MyFun.bytes2U8str(k), ",", MyFun.bytes2U8str(v))));

		System.out.println("==3 队列推拉===");
		_db.push("测试", "1".getBytes());
		_db.push("测试", "的".getBytes());
		_db.push("测试", "2".getBytes());
		_db.push("测试", "_".getBytes());
		_db.push("测试", "3".getBytes());
		_db.push("测试", "a".getBytes());
		_db.values("测试".getBytes()).forEach((v) -> System.out.println(MyFun.bytes2U8str(v)));
		for (int i = 0; i < 6; i++) {
			byte[] kv3 = _db.pull("测试");
			if (kv3 == null)
				System.out.println("为空");

			System.out.println(MyFun.join("取出", MyFun.bytes2U8str(kv3)));
		}
		_db.values("测试".getBytes()).forEach((v) -> System.out.println(MyFun.bytes2U8str(v)));

		System.out.println("==4.1 取出===");
		_db.clear();
		_db.putBs("p/user/".getBytes(), "a", "aaa".getBytes());
		_db.putBs("p/user/".getBytes(), "b", "bbb".getBytes());
		_db.putBs("p/user/".getBytes(), "c", "ccc".getBytes());

		_db.take连续Sb("p/user/", call1);
		System.out.println("==4.2 取出===");
		_db.clear();
		_db.putBs("p/user/".getBytes(), "a", "aaa".getBytes());
		_db.putBs("p/user/".getBytes(), "b", "bbb".getBytes());
		_db.putBs("p/user/".getBytes(), "c", "ccc".getBytes());

		_db.take连续Ss("p/user/", call2);
		System.out.println("==4.3 取出===");
		_db.clear();
		_db.putBt("p/user/".getBytes(), "aaa".getBytes());
		_db.putBt("p/user/".getBytes(), "bbb".getBytes());
		_db.putBt("p/user/".getBytes(), "ccc".getBytes());

		_db.take连续SL("p/user/", call4);

		System.out.println("==4.4 取出===");
		_db.clear();
		_db.putBt("p/user/".getBytes(), "aaa".getBytes());
		_db.putBt("p/user/".getBytes(), "bbb".getBytes());
		_db.putBt("p/user/".getBytes(), "ccc".getBytes());

		_db.removeTop("p/user/".getBytes());
		maps = _db.getAll();
		maps.forEach((k, v) -> System.out.println(MyFun.join(MyFun.bytes2U8str(k), ",", MyFun.bytes2U8str(v))));

		System.out.println("==6 批量===");
		_db.clear();
		_db.putManyB(map);
		maps = _db.getAll();
		maps.forEach((k, v) -> System.out.println(MyFun.join(MyFun.bytes2U8str(k), ",", MyFun.bytes2U8str(v))));
		System.out.println("==6.1 批量===");
		_db.clear();

		_db.putManyS(map1);
		maps = _db.getAll();
		maps.forEach((k, v) -> System.out.println(MyFun.join(MyFun.bytes2U8str(k), ",", MyFun.bytes2U8str(v))));
		System.out.println("==6.2 批量===");
		_db.clear();

		_db.putManyB(list1);
		list = _db.values();
		list.forEach((v) -> System.out.println(MyFun.bytes2U8str(v)));
		n = MyFun.getMs() - n;
		// System.out.println(n);
		System.out.println("==6.2.1 批量===");
		_db.clear();
		_db.putManyS(list2);
		list = _db.values();
		list.forEach((v) -> System.out.println(MyFun.bytes2U8str(v)));

		System.out.println("==6.3.1 批量===");
		_db.clear();
		_db.putManyBt("测试aa".getBytes(), list1);
		list = _db.values();
		list.forEach((v) -> System.out.println(MyFun.bytes2U8str(v)));

		System.out.println("==6.3.2 批量===");
		_db.clear();
		_db.putManyBn("测试aa".getBytes(), 0, list1);
		maps = _db.getAll();
		maps.forEach((k, v) -> System.out.println(MyFun.join(MyFun.bytes2U8str(k), ",", MyFun.bytes2U8str(v))));

		System.out.println("==6.3.3 批量===");
		_db.clear();
		_db.putManySb("测试ab", map2);

		maps = _db.getAll();
		maps.forEach((k, v) -> System.out.println(MyFun.join(MyFun.bytes2U8str(k), ",", MyFun.bytes2U8str(v))));

		System.out.println("==6.3.4 批量===");
		_db.clear();
		_db.putManySs("测试c", map1);

		maps = _db.getAll();
		maps.forEach((k, v) -> System.out.println(MyFun.join(MyFun.bytes2U8str(k), ",", MyFun.bytes2U8str(v))));
		System.out.println("==6.3.5 批量===");
		_db.clear();
		_db.putManySi("测试c", map4);

		maps = _db.getAll();
		maps.forEach((k, v) -> System.out.println(MyFun.join(MyFun.bytes2U8str(k), ",", MyFun.bytes2U8str(v))));

		System.out.println("==6.4 批量===");
		_db.clear();
		_db.putManyBo("学生".getBytes(), list3);
		List<User> list4 = _db.getLikeBo("学生".getBytes());
		list4.forEach((v) -> System.out.println(v.toString()));

		_db.clear();
		_db.putManySb("测试ab", map2);
		System.out.println("==7.1 查询===");

		_db.leftBb("测试".getBytes(), call1);
		System.out.println("==7.2 查询===");

		_db.leftSb("测试", call1);
		System.out.println("==7.3 查询===");
		_db.leftSs("测试", call2);
		System.out.println("==7.4 查询===");
		_db.clear();
		_db.putManySi("测试c", map4);
		_db.leftSi("测试c", call3);

		n = MyFun.getMs() - n;
		// System.out.println(n);
	}
}
